package com.linkit.main.setting;

import java.util.List;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkit.config.MediaType;
import com.linkit.config.Setting;
import com.linkit.main.MainActivity;
import com.linkit.main.R;
import com.linkit.utils.ScreenUtils;
import com.linkit.utils.Utils;

/**
 * adapter hiện thị danh sách các hãng xe trong tuyến
 * */
public class TypeAdapter extends BaseAdapter implements OnItemClickListener {

	private MainActivity context;
	private MediaType[] items;
	private List<String> ids;
	private boolean isSelectedAll;

	public TypeAdapter(MediaType[] items) {
		this.context = MainActivity.getInstance();
		this.items = items;
		ids = Utils.parseString(Setting.getTypes());
		isSelectedAll = Utils.isSelectedAll(ids);
	}

	public class ViewHolder {
		private TextView domain;
		private ImageView imageView;
		private MediaType site;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		MediaType rowItem = getItem(position);

		LayoutInflater mInflater = context.getLayoutInflater();
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.setting_type_item, null);
			holder = new ViewHolder();
			holder.domain = (TextView) convertView
					.findViewById(R.id.setting_site_domain);

			holder.imageView = (ImageView) convertView
					.findViewById(R.id.setting_site_selected);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.domain.setText(rowItem.getName());
		holder.domain.setTextSize(ScreenUtils.getDialogItemFontSize());
		holder.site = rowItem;

		int resId = R.drawable.checkbox;
		rowItem.setSelected(false);
		if (isSelectedAll) {
			resId = R.drawable.checkboxfocus;
			rowItem.setSelected(true);
		} else if (ids != null) {
			int id;
			for (int i = 0; i < ids.size(); i++) {
				id = Integer.parseInt(ids.get(i));
				if (id == holder.site.getId()) {
					resId = R.drawable.checkboxfocus;
					rowItem.setSelected(true);
					break;
				}
			}

		}

		holder.imageView.setImageResource(resId);
		return convertView;
	}

	@Override
	public int getCount() {
		return items.length;
	}

	@Override
	public MediaType getItem(int position) {
		return items[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.site.setSelected(!holder.site.isSelected());

		if (holder.site.isSelected()) {
			holder.imageView.setImageResource(R.drawable.checkboxfocus);
		} else {
			holder.imageView.setImageResource(R.drawable.checkbox);
		}

		// Lưu config
		StringBuffer sBuffer = new StringBuffer("");
		MediaType[] mTypes = MediaType.values();
		for (int i = 0; i < mTypes.length; i++) {
			if (mTypes[i].isSelected()) {
				sBuffer.append(mTypes[i].getId() + Setting.SPACE);
			}
		}

		if (sBuffer.length() >= 1) {
			Setting.setType(sBuffer.substring(0, sBuffer.length() - 1));
		} else {
			Setting.setType("");
		}

		Log.e("", "type ===" + Setting.getTypes());
	}
}